<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
    >
      <el-form-item label="模板名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模板名称"
          @change="searchchange"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题型名称" prop="beanName">
        <el-select
          v-model="queryParams.beanName"
          filterable
          allow-create
          default-first-option
          placeholder="请选择题型名称"
          clearable
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item2, index) in typeOptions"
            :key="index"
            :label="item2.dictLabel"
            :value="item2.dictValue"
            :disabled="item2.disabled"
          ></el-option>
        </el-select>
        <!-- <el-select v-model="queryParams.type" placeholder="请选择题型名称" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select> -->
      </el-form-item>
      <el-form-item label="题型数量" prop="count">
        <el-input
          v-model.number="queryParams.count"
          placeholder="请输入题型数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小题分值" prop="questionScore">
        <el-input
          v-model.number="queryParams.questionScore"
          placeholder="请输入小题分值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="评分模式"
        prop="multipleFlag"
        v-if="queryParams.beanName == 'multipleChoiceServiceImpl'"
      >
        <el-select
          v-model="queryParams.multipleFlag"
          filterable
          allow-create
          default-first-option
          placeholder="请选择评分模式"
          clearable
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item, index) in formworkOptions2"
            :key="index"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="queryParams.multipleFlag == 2"
        label="漏选固定分"
        prop="multipleFraction"
      >
        <el-input
          v-model.trim="queryParams.multipleFraction"
          placeholder="请输入漏选固定分分值"
          clearable
          :style="{ width: '100%' }"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['topic:template:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['topic:template:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['topic:template:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['topic:template:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="templateList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="小题分值" align="center" prop="id" />-->
      <el-table-column label="模板名称" align="center" prop="name" />
      <el-table-column label="题型名称" align="center" prop="type" />
      <el-table-column label="题型数量" align="center" prop="count" />
      <el-table-column label="小题分值" align="center" prop="questionScore" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['topic:template:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['topic:template:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改题型模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="88px">
        <el-form-item label="模板名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入模板名称" />
        </el-form-item>

        <el-form-item label="题型名称" prop="beanName">
          <el-select
            v-model="form.beanName"
            default-first-option
            placeholder="请选择题型名称"
            clearable
            @change="moreselectchange"
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in typeOptions"
              :key="index"
              :label="item.dictLabel"
              :value="item.dictValue"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="题型数量"
          prop="count"
          v-if="form.beanName == 'bigQuestionServiceImpl'"
        >
          <el-input
            v-model="form.count"
            type="number"
            placeholder="请输入小题数量"
          />
        </el-form-item>
        <el-form-item
          label="选项数量"
          prop="count"
          v-if="form.beanName == 'singleChoiceServiceImpl'"
        >
          <el-input
            v-model="form.count"
            type="number"
            placeholder="请输入单选题数量"
          />
        </el-form-item>
        <el-form-item
          label="选项数量"
          prop="count"
          v-if="form.beanName == 'multipleChoiceServiceImpl'"
        >
          <el-input
            v-model="form.count"
            type="number"
            placeholder="请输入多选题数量"
          />
        </el-form-item>
        <el-form-item
          label="填空数量"
          prop="count"
          v-if="
            form.beanName == 'completionServiceImpl' ||
            form.beanName == 'bankedClozeServiceImpl'
          "
        >
          <el-input
            v-model="form.count"
            type="number"
            placeholder="请输入填空题数量"
          />
        </el-form-item>

        <el-form-item label="题目分值" type="number" prop="questionScore">
          <el-input v-model="form.questionScore" placeholder="请输入题目分值" />
        </el-form-item>
        <el-form-item
          label="评分模式"
          prop="multipleFlag"
          v-if="form.beanName == 'multipleChoiceServiceImpl'"
        >
          <el-select
            v-model="form.multipleFlag"
            filterable
            allow-create
            default-first-option
            placeholder="请选择评分模式"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in formworkOptions2"
              :key="index"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          v-if="form.multipleFlag == 2"
          label="漏选固定分"
          prop="multipleFraction"
        >
          <el-input
            v-model.trim="form.multipleFraction"
            placeholder="请输入漏选固定分分值"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <!-- <div class="clearfix">
        <span class="right"> -->
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
        <!-- </span>
        </div> -->
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTemplate,
  getTemplate,
  delTemplate,
  addTemplate,
  updateTemplate,
  exportTemplate,
} from '@/api/topic/template'

export default {
  name: 'Template',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 题型模板表格数据
      templateList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        beanName: null,
        type: null,
        count: null,
        questionScore: null,
        multipleFlag: null,
        multipleFraction: null,
      },
      //题型
      typeOptions: [],
      //多选评分模式
      formworkOptions2: [
        {
          label: '全对给分',
          value: 0,
        },
        {
          label: '漏选给分',
          value: 1,
        },
        {
          label: '漏选给固定分',
          value: 2,
        },
      ],
      // 表单参数
      form: {
        name: null,
        type: null,
        count: null,
        beanName: null,
        questionScore: null,
        multipleFlag: null,
        multipleFraction: null,
      },
      // 表单校验
      rules: {},
    }
  },
  created() {
    this.getList()
    this.getDicts('question_type').then((response) => {
      this.typeOptions = response.data
    })
  },

  methods: {
    /** 查询题型模板列表 */
    getList() {
      this.loading = true
      listTemplate(this.queryParams).then((response) => {
        this.templateList = response.rows
        console.log('templateList:', this.templateList)
        this.total = response.total
        this.loading = false
      })
    },
    moreselectchange(val) {
      // this.form.type = val
      var that = this;
      for (var i = 0; i < that.typeOptions.length; i++) {
        if (that.form.beanName == that.typeOptions[i].dictValue) {
          that.form.type = that.typeOptions[i].dictLabel;
        }
      }
    },
    searchchange() {
      var that = this
      for (var i = 0; i < that.typeOptions.length; i++) {
        if (that.queryParams.beanName == that.typeOptions[i].dictValue) {
          that.queryParams.type = that.typeOptions[i].dictLabel
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        count: null,
        beanName: null,
        questionScore: null,
        multipleFlag: null,
        multipleFraction: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.queryParams.multipleFraction = parseFloat(
        this.queryParams.multipleFraction
      )
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加题型模板'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTemplate(id).then((response) => {
        this.form = response.data
        console.log(response.data)
        this.open = true
        this.title = '修改题型模板'
      })
    },
    /** 提交按钮 */
    submitForm() {
      //把分数和数量都转成flaot
      if (this.form.multipleFlag != 2) {
        this.form.multipleFraction = 0
      }
      this.form.count = parseInt(this.form.count)
      this.form.questionScore = parseFloat(this.form.questionScore)
      this.form.multipleFraction = parseFloat(this.form.multipleFraction)
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTemplate(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addTemplate(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm(
        '是否确认删除题型模板编号为"' + ids + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(function () {
          return delTemplate(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有题型模板数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.exportLoading = true
          return exportTemplate(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>
